/*
* Filename:
* Summary:	
*
* Author:
* Version:
* Finished time:
*/
#include "head.h"
#include "config.h"
#include "ipmsg.h"
#include "treatmess.h"
#include "mainwindow.h"
#include "utility.h"
#include "recdialog.h"
#include "network.h"
#include "recdialog.h"
/*
*sub function
*/
gboolean CheckExist(GtkTreeModel *model, GtkTreePath *path,
		    GtkTreeIter *iter, gpointer data)
{
#ifdef _DEBUG
	printf("Enter CheckExit\n");
#endif
	gchar *ipaddress;
	struct PassAddress *temp = (struct PassAddress *)data;
	gtk_tree_model_get(model, iter, IPADDRESS, &ipaddress, -1);

#ifdef _DEBUG
	printf("Get from model:%s\n, From outside:%s\n", ipaddress, temp->ip);
#endif

	int i = strcmp(ipaddress, temp->ip);
	if ( i == 0 )
	{
		temp->result = 0;
	}

	free(ipaddress);
	return FALSE;
}

/*
* Function:TreatNoOperation
* Summary:
*
* Argument:
* 1th argument --
* 2th argument --
*
* Return:
*  
*/
void TreatNoOperation(struct sockaddr *from, struct MessageFormat *mess)
{
#ifdef _DEBUG
	printf("Enter TreatNoOperation\n");
#endif
}

/*
* Function:
* Summary:
*
* Argument:
* 1th argument --
* 2th argument --
*
* Return:
*  
*/
void TreatBrEntry(struct sockaddr *from, struct MessageFormat *mess)
{
#ifdef _DEBUG
	printf("Enter TreatBrEntry\n");
#endif
	char buf[MAX_MESSAGE] = {'\0'};
	int size = AssembleMessage(buf, IPMSG_ANSENTRY, NULL, 0);

	int err = sendto(globalfd, buf, size + 1, 0, 
			 from, sizeof(struct sockaddr_in));
#ifdef _DEBUG
	if ( err < 0 )
	{
		perror("Error in TreatBrEntry\n");
	} else
	{
		printf("TreatBrEntry Succeed\n");
	}
#endif

//as same as Asnentry
	gchar *login_name = ConvertFrom(mess->login_name);
	gchar *host_name = ConvertFrom(mess->host_name);
	gchar *nick_name = ConvertFrom(mess->append_message);
	gchar *nick_name_for_empty = nick_name;
	int name_len = strlen(mess->append_message);
	gchar *group = mess->append_message + name_len + 1;
	gchar *group_name = ConvertFrom(group);
	struct sockaddr_in *from_in = (struct sockaddr_in *)from;
	int address = from_in->sin_addr.s_addr;
	char *ipaddress = inet_ntoa(from_in->sin_addr);
	int port = ntohs(from_in->sin_port);
	//if user doesn't set nick name , replace it by login name
	if ( strlen(nick_name) == 0 )
	{
		nick_name_for_empty = login_name;
	}
	//check if this item exists
#ifdef _DEBUG
	g_printf("login_name:%s host_name:%s nick_name:%s group_name:%s ip:%s port:%d\n",
		 login_name, host_name, nick_name, group_name, ipaddress, port);
#endif
	struct PassAddress value;
	memset(&value, '\0', sizeof(struct PassAddress));
	value.result = 1;
	value.ip = ipaddress;
	GtkTreeIter iter;

	if ( user_num == 0 )
	{
		gtk_list_store_append(list_store, &iter);
		gtk_list_store_set(list_store, &iter, NICKNAME, nick_name_for_empty,
				   GROUP, group_name, IPADDRESS, ipaddress,
				   LOGINNAME, login_name, HOSTNAME, host_name,
				   PORT, port, -1);//don't forget -1
		user_num ++;
	} else
	{

		gtk_tree_model_foreach(GTK_TREE_MODEL(list_store), CheckExist, &value);
#ifdef _DEBUG
		printf("Not existed?:%d\n", value.result);
#endif
		if ( value.result )
		{

			gtk_list_store_append(list_store, &iter);
			gtk_list_store_set(list_store, &iter, NICKNAME, nick_name_for_empty,
					   GROUP, group_name, IPADDRESS, ipaddress,
					   LOGINNAME, login_name, HOSTNAME, host_name,
					   PORT, port, -1);//don't forget -1
			user_num ++;
		}

	}
#ifdef _DEBUG
	printf("user_num:%d\n", user_num);
#endif
	sprintf(usernum, "%d", user_num);
	gtk_label_set_text(GTK_LABEL(num_label), usernum);

	free(login_name);
	free(nick_name);
	free(group_name);
	free(host_name);
}
/*
* Function:
* Summary:
*
* Argument:
* 1th argument --
* 2th argument --
*
* Return:
*  
*/
void TreatAnsEntry(struct sockaddr *from, struct MessageFormat *mess)
{
#ifdef _DEBUG
	printf("Enter TreatAnsEntry\n");
#endif
	gchar *login_name = ConvertFrom(mess->login_name);
	gchar *host_name = ConvertFrom(mess->host_name);
	gchar *nick_name = ConvertFrom(mess->append_message);
	gchar *nick_name_for_empty = nick_name;
	int name_len = strlen(mess->append_message);
	gchar *group = mess->append_message + name_len + 1;
	gchar *group_name = ConvertFrom(group);
	struct sockaddr_in *from_in = (struct sockaddr_in *)from;
	int address = from_in->sin_addr.s_addr;
	char *ipaddress = inet_ntoa(from_in->sin_addr);
	int port = ntohs(from_in->sin_port);
	//if user doesn't set nick name , replace it by login name
	if ( strlen(nick_name) == 0 )
	{
		nick_name_for_empty = login_name;
	}
	//check if this item exists
#ifdef _DEBUG
	g_printf("login_name:%s host_name:%s nick_name:%s group_name:%s ip:%s port:%d\n",
		 login_name, host_name, nick_name, group_name, ipaddress, port);
#endif
	struct PassAddress value;
	memset(&value, '\0', sizeof(struct PassAddress));
	value.result = 1;
	value.ip = ipaddress;
	GtkTreeIter iter;

	if ( user_num == 0 )
	{
		gtk_list_store_append(list_store, &iter);
		gtk_list_store_set(list_store, &iter, NICKNAME, nick_name_for_empty,
				   GROUP, group_name, IPADDRESS, ipaddress,
				   LOGINNAME, login_name, HOSTNAME, host_name,
				   PORT, port, -1);//don't forget -1
		user_num ++;
	} else
	{

		gtk_tree_model_foreach(GTK_TREE_MODEL(list_store), CheckExist, &value);
#ifdef _DEBUG
		printf("Not existed?:%d\n", value.result);
#endif
		if ( value.result )
		{

			gtk_list_store_append(list_store, &iter);
			gtk_list_store_set(list_store, &iter, NICKNAME, nick_name_for_empty,
					   GROUP, group_name, IPADDRESS, ipaddress,
					   LOGINNAME, login_name, HOSTNAME, host_name,
					   PORT, port, -1);//don't forget -1
			user_num ++;
		}

	}
#ifdef _DEBUG
	printf("user_num:%d\n", user_num);
#endif
	sprintf(usernum, "%d", user_num);
	gtk_label_set_text(GTK_LABEL(num_label), usernum);

	free(login_name);
	free(nick_name);
	free(group_name);
	free(host_name);
}

/*
* Function:TreatSendMsg()
* Summary:Treat the text message and show them
*
* Argument:
* 1th argument --
* 2th argument --
*
* Return:
*  
*/
void TreatSendMsg(struct sockaddr *from, struct MessageFormat *mess)
{
	char buf[MAX_MESSAGE] = {'\0'};
	memset(buf, '\0', MAX_MESSAGE);
	char append_buf[MAX_APPEND_MESSAGE] = {'\0'};
	memset(append_buf, '\0', MAX_APPEND_MESSAGE);
#ifdef _DEBUG
	printf("Enter TreatBrEntry\n");
#endif
	MakeRecDialog(mess, (struct sockaddr_in *)from);

	int sz = ConstructAppendBuf(append_buf);
	int size = AssembleMessage(buf, IPMSG_RECVMSG, append_buf, sz);

	int err = sendto(globalfd, buf, size + 1, 0, 
			 from, sizeof(struct sockaddr_in));
#ifdef _DEBUG
	if ( err < 0 )
	{
		perror("Error in TreatSendMsg\n");
	} else
	{
		printf("Send echo message(IPMSG_RECVMSG) success\n\n");
		printf("TreatSendMsg Succeed\n");
	}
#endif
}
/*
* Function:TreatSendMsg()
* Summary:Echo when receive the SendMSG
*
* Argument:
* 1th argument --
* 2th argument --
*
* Return:
*  
*/

void TreatRecvMsg(struct sockaddr *from, struct MessageFormat *mess)
{
#ifdef _DEBUG
	printf("Enter TreatRecvMsg\n");
#endif
}
